package com.lee.dutylower.view.wiget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.lee.dutylower.MyApplication;
import com.lee.dutylower.R;
import com.lee.dutylower.db.greenEntity.Squad;
import com.lee.dutylower.db.greenEntity.SquadRecord;
import com.lee.dutylower.db.greendao.SquadDao;
import com.lee.dutylower.db.greendao.SquadRecordDao;
import com.lee.dutylower.domain.SquadTypeEnum;
import com.lee.dutylower.utils.Globals;
import com.lee.dutylower.utils.LogUtils;
import com.lee.dutylower.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijie on 2017/5/15.
 */

public class ScheduleView extends LinearLayout {
    public TextView tvNoData;
    public TextView tvName;
    public ListView lv;
    private IndexAdapter indexAdapter;
    private ArrayList<Squad> values;
    public AlertDialog alert;
    private ListAdapter listAdpter;
    private List<Squad> DBvalues;
    private SquadDao squadDao;
    private SquadRecordDao srDao;


    public ScheduleView(Context context) {
        super(context);
        initView();
    }

    public ScheduleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ScheduleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    public void initView(){
        //设置自定义group开启ondraw 默认关闭
        setWillNotDraw(false);

        squadDao = MyApplication.getApplication().getDaoSession().getSquadDao();
        srDao = MyApplication.getApplication().getDaoSession().getSquadRecordDao();
        DBvalues = new ArrayList<>();
        values = new ArrayList<>();

        View v = View.inflate(getContext(), R.layout.schedule_view_wiget,null);
        tvName = (TextView) v.findViewById(R.id.tv_schedule_index);
        tvNoData = (TextView) v.findViewById(R.id.tv_schedule_index_null);
        lv = (ListView) v.findViewById(R.id.lv_schedule_index);
        indexAdapter = new IndexAdapter();
        lv.setAdapter(indexAdapter);
        addView(v);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void updateList(ArrayList<Squad> values){
        if (values == null){
            this.values.clear();
        }else {
            this.values = values;
        }
        indexAdapter.notifyDataSetChanged();
    }

    public void updateTextInfo(boolean hasData,String info){
        if (hasData){
            tvNoData.setVisibility(GONE);
            tvNoData.setClickable(false);
        }else {
            tvNoData.setVisibility(VISIBLE);
            tvNoData.setText(info);
            tvNoData.setClickable(true);
            tvNoData.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (tvName.getText().toString()) {
                        case "上岗班":
                            Globals.ON.clear();
                            break;
                        case "值班班":
                            Globals.DUTY.clear();
                            break;
                        case "补岗班":
                            Globals.REST.clear();
                            break;
                    }

                    DBvalues = squadDao.queryBuilder().build().list();
                    listAdpter = new ListAdapter();
                    alert = new AlertDialog.Builder(getContext())
                            .setView(initDialogView())
                            .setCancelable(true)
                            .setTitle("选择班")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    switch (tvName.getText().toString()) {
                                        case "上岗班":
                                            if (Globals.ON.size() != 0){
                                                for (Squad squad : Globals.ON) {
                                                    SquadRecord sr = new SquadRecord(null,squad.getSuqadId(), SquadTypeEnum.OnSquad.getType());
                                                    srDao.insert(sr);
                                                }
                                            }
                                            values.clear();
                                            values.addAll(Globals.ON);

                                            break;
                                        case "值班班":
                                            if (Globals.DUTY.size() != 0){
                                                for (Squad squad : Globals.DUTY) {
                                                    SquadRecord sr = new SquadRecord(null,squad.getSuqadId(),SquadTypeEnum.DutySquad.getType());
                                                    srDao.insert(sr);
                                                }
                                            }
                                            values.clear();
                                            values.addAll(Globals.DUTY);

                                            break;
                                        case "补岗班":
                                            if (Globals.REST.size() != 0){
                                                for (Squad squad : Globals.REST) {
                                                    SquadRecord sr = new SquadRecord(null,squad.getSuqadId(),SquadTypeEnum.RestSquad.getType());
                                                    srDao.insert(sr);
                                                }
                                            }
                                            values.clear();
                                            values.addAll(Globals.REST);

                                            break;
                                    }
                                    indexAdapter.notifyDataSetChanged();
                                    updateTextInfo(true,"");
                                }
                            })
                            .setNegativeButton("取消",null)
                            .create();
                    alert.show();
                }
            });
        }
    }

    public void setItem(String item){
        tvName.setText(item);
    }

    class IndexAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = View.inflate(getContext(),R.layout.schedule_listview_item,null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tv_lv_schedule_item);
            tv.setText(values.get(position).getSquadName());
            return convertView;
        }
    }

    //初始化班级列表
    private View initDialogView() {
        final View dialogview = View.inflate(getContext(), R.layout.dialog_setting_list,null);
        ListView listView = (ListView) dialogview.findViewById(R.id.lv_dialog);
        listView.setAdapter(listAdpter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view.findViewById(R.id.tv_lv_data);
                LogUtils.e("ScheduleView","click");
                switch (tvName.getText().toString()){
                    case "上岗班":
                        if (iscontain(Globals.DUTY,DBvalues.get(position)) || iscontain(Globals.REST,DBvalues.get(position))){
                            ToastUtils.showShort(getContext(),"该班级已被添加");
                            return;
                        }else {
                            if(Globals.ON.contains(DBvalues.get(position))){
                                tv.setBackgroundColor(Color.TRANSPARENT);
                                Globals.ON.remove(DBvalues.get(position));
                            }else {
                                tv.setBackgroundColor(0xFFA36F59);
                                Globals.ON.add(DBvalues.get(position));
                            }
                        }
                        break;
                    case "值班班":
                        if (iscontain(Globals.ON,DBvalues.get(position)) || iscontain(Globals.REST,DBvalues.get(position))){
                            ToastUtils.showShort(getContext(),"该班级已被添加");
                            return;
                        }else {
                            if(Globals.DUTY.contains(DBvalues.get(position))){
                                tv.setBackgroundColor(Color.TRANSPARENT);
                                Globals.DUTY.remove(DBvalues.get(position));
                            }else {
                                tv.setBackgroundColor(0xFFA36F59);
                                Globals.DUTY.add(DBvalues.get(position));
                            }
                        }
                        break;
                    case "补岗班":
                        if (iscontain(Globals.ON,DBvalues.get(position)) || iscontain(Globals.DUTY,DBvalues.get(position))){
                            ToastUtils.showShort(getContext(),"该班级已被添加");
                            return;
                        }else {
                            if(Globals.REST.contains(DBvalues.get(position))){
                                tv.setBackgroundColor(Color.TRANSPARENT);
                                Globals.REST.remove(DBvalues.get(position));
                            }else {
                                tv.setBackgroundColor(0xFFA36F59);
                                Globals.REST.add(DBvalues.get(position));
                            }
                        }
                        break;
                }
            }
        });
        return dialogview;
    }

    public boolean iscontain(List<Squad> values, Squad squadDB){
        for (Squad value : values) {
            if (value.getSuqadId() == squadDB.getSuqadId()) return true;
        }
        return false;
    }

    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return DBvalues.size();
        }

        @Override
        public Object getItem(int position) {
            return DBvalues.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Squad squad = DBvalues.get(position);
            if(convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(getContext(),R.layout.dialog_listview_item,null);
                holder.tv = (TextView) convertView.findViewById(R.id.tv_lv_data);
                holder.ID = (TextView) convertView.findViewById(R.id.tv_lv_id);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(squad.getSquadName());
            holder.ID.setText(squad.getSuqadId()+"");
            return convertView;
        }

        class ViewHolder{
            TextView tv;
            TextView ID;
        }
    }
}
